package com.bfly.mall.advert.controller;

import com.bfly.common.ResponseUtil;
import com.bfly.common.page.Pager;
import com.bfly.common.util.DataConvertUtils;
import com.bfly.core.base.controller.BaseController;
import com.bfly.core.context.PagerThreadLocal;
import com.bfly.core.security.ActionModel;
import com.bfly.mall.advert.entity.Advert;
import com.bfly.mall.advert.dto.AdvertFormDTO;
import com.bfly.mall.advert.dto.AdvertListDTO;
import com.bfly.mall.advert.service.IAdvertService;
import com.bfly.core.base.entity.ResponseData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 广告Advert
 *
 * @author 长沙商脉科技有限公司
 * @date 2020/9/18 22:48
 */
@RestController
@RequestMapping(value = "/manage/advert")
public class AdvertController extends BaseController {

    @Autowired
    private IAdvertService advertService;

    /**
     * 广告列表
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/9/18 22:48
     */
    @GetMapping(value = "/list")
    public void list(HttpServletResponse response, HttpServletRequest request) {
        PagerThreadLocal.set(request);
        String enable = request.getParameter("enable");
        String type = request.getParameter("type");
        Map<String, Object> param = new HashMap<>(2);
        if (StringUtils.isNotBlank(enable)) {
            param.put("is_enable", DataConvertUtils.convertToBoolean(enable));
        }
        if (StringUtils.isNotBlank(type)) {
            param.put("type", type);
        }
        Pager<Advert> pager = advertService.getPage(param);
        Pager<AdvertListDTO> dtoPager = new Pager<>(pager.getPageNo(), pager.getPageSize(), pager.getTotalCount());

        if (pager.getData() != null) {
            List<AdvertListDTO> list = new ArrayList<>();
            for (Advert advert : pager.getData()) {
                AdvertListDTO dto = new AdvertListDTO();
                BeanUtils.copyProperties(advert, dto);
                list.add(dto);
            }
            dtoPager.setData(list);
        }
        ResponseUtil.writeJson(response, ResponseData.getSuccess(dtoPager));
    }

    /**
     * 新增广告
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/9/20 12:24
     */
    @PostMapping(value = "/add")
    @ActionModel(value = "新增广告")
    public void saveAdvert(HttpServletResponse response, @Valid @RequestBody AdvertFormDTO formDTO, BindingResult result) {
        validData(result);
        advertService.save(formDTO);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }

    /**
     * 编辑广告
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/9/20 12:24
     */
    @PostMapping(value = "/edit")
    @ActionModel(value = "编辑广告")
    public void editAdvert(HttpServletResponse response, @Valid @RequestBody AdvertFormDTO formDTO, BindingResult result) {
        validData(result);
        advertService.edit(formDTO);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }

    /**
     * 删除广告
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/9/20 12:26
     */
    @PostMapping(value = "/delete")
    @ActionModel(value = "删除广告")
    public void removeAdvert(HttpServletResponse response, @RequestBody Integer... adIds) {
        advertService.remove(adIds);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }
}